package function;

import common.GetLines;
import common.PostJson;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class OrderAction {

	private static String GET_UNFINISHED_ORDER = "http://106.15.43.138:8080/api/order/unfini_lists ";
	private static String SUBMIT_ORDER = "http://106.15.43.138:8080/api/order/submit";
	private static String CANCEL_ORDER = "http://106.15.43.138:8080/api/order/passenger_cancel ";
	private static String GET_ORDER_DETAIL = "http://106.15.43.138:8080/api/order/detail";
	private static String GET_COUPON = "http://106.15.43.138:8080/api/coupon/getlist";
	private static String GET_USABLE_COUPON = "http://106.15.43.138:8080/api/coupon/getusable2";
	private static String GET_AVAILABLE_SALE = "http://106.15.43.138:8080/api/price/get_usable_sale";
	private static String EVALUATE_ORDER = "http://106.15.43.138:8080/api/order/evaluate";
	private static String SCHEDULE_TIME = "http://106.15.43.138:8080/api/order_ai/schedule_time";
	private static String GET_ORDERS_ALL = "http://106.15.43.138:8080/api/order/lists_new";

	// 获取未完成订单√
	public static String getUnfinishedOrder(JSONObject json) {
		json.put("app", "0");
		String aJson = PostJson.postJson(json, GET_UNFINISHED_ORDER);
		return aJson;
	}

	// 获取所有订单
	public static String getAllOrders(JSONObject json) {
		String aJson = PostJson.postJson(json, GET_ORDERS_ALL);
		return aJson;
	}

	// 提交订单√
	public static String submitOrder(JSONObject json) {
		String aJson = PostJson.postJson(json, SUBMIT_ORDER);
		return aJson;
	}

	// 取消订单√
	public static String cancelOrder(JSONObject json) {
		json.put("reason_type", 0);
		String aJson = PostJson.postJson(json, CANCEL_ORDER);
		return aJson;
	}

	// 查看订单详情√
	public static String getOrderDetail(JSONObject json) {
		String aJson = PostJson.postJson(json, GET_ORDER_DETAIL);
		return aJson;
	}

	// 优惠券
	public static String getCouponList(JSONObject json) {
		String aJson = PostJson.postJson(json, GET_COUPON);
		JSONObject bJson = JSONObject.fromObject(aJson);
		if(!bJson.getString("status").equals("0"))
		{
			return aJson;
		}
		JSONArray coupons = bJson.getJSONArray("coupons");
		JSONArray jaCoupons = new JSONArray();
		for (int i = 0; i < coupons.size(); i++) {
			String str = coupons.getString(i);
			int index = str.indexOf("\"");
			int lastIndex = str.lastIndexOf("\"");
			String aStr = str.substring(index + 1, lastIndex);
			JSONObject coupon = JSONObject.fromObject(aStr);
			JSONObject metadata = coupon.getJSONObject("metadata");
			if (metadata.getString("usableline").equals("[\"*\"]")) {
				metadata.remove("usableline");
				metadata.put("usableline", "通用");
			} else {
				String line = metadata.getString("usableline");
				JSONArray jaLine = JSONArray.fromObject(line);
				JSONArray lineName = new JSONArray();
				for (int j = 0; j < jaLine.size(); j++) {
					lineName.add(GetLines.getLines(jaLine.getString(j)));
				}
				metadata.remove("usableline");
				metadata.put("usableline", lineName);
			}
			if (metadata.getString("car_class").equals("[\"*\"]")) {
				metadata.remove("car_class");
				metadata.put("car_class", "通用");
			} else {
				String carClass = metadata.getString("car_class");
				JSONArray jaLine = JSONArray.fromObject(carClass);
				metadata.remove("car_class");
				metadata.put("car_class", jaLine);
			}
			jaCoupons.add(coupon);
		}
		return jaCoupons.toString();
	}

	// 获取所有优惠券（下单时用）
	public static String usableCoupon(JSONObject json) {
		String aJson = PostJson.postJson(json, GET_USABLE_COUPON);
		JSONObject bJson = JSONObject.fromObject(aJson);
		if(!bJson.getString("status").equals("0"))
		{
			return aJson;
		}
		JSONArray coupons = bJson.getJSONArray("coupons");
		JSONArray jaCoupons = new JSONArray();
		for (int i = 0; i < coupons.size(); i++) {
			String str = coupons.getString(i);
			JSONObject coupon = JSONObject.fromObject(str);
			JSONObject metadata = coupon.getJSONObject("metadata");
			if (metadata.getString("usableline").equals("[\"*\"]")) {
				metadata.remove("usableline");
				metadata.put("usableline", "通用");
			} else {
				String line = metadata.getString("usableline");
				JSONArray jaLine = JSONArray.fromObject(line);
				JSONArray lineName = new JSONArray();
				for (int j = 0; j < jaLine.size(); j++) {
					lineName.add(GetLines.getLines(jaLine.getString(j)));
				}
				metadata.remove("usableline");
				metadata.put("usableline", lineName);
			}
			if (metadata.getString("car_class").equals("[\"*\"]")) {
				metadata.remove("car_class");
				metadata.put("car_class", "通用");
			} else {
				String carClass = metadata.getString("car_class");
				JSONArray jaLine = JSONArray.fromObject(carClass);
				metadata.remove("car_class");
				metadata.put("car_class", jaLine);
			}
			jaCoupons.add(coupon);
		}		
		int maxSum = 0;
		Integer aIndex = 0;
		int flag = 0;
		for (int j = 0; j < jaCoupons.size(); j++) {
			JSONObject jaCoupon = jaCoupons.getJSONObject(j);
			if (jaCoupon.getString("valid").equals("1")) {
				int sum = Integer.parseInt(jaCoupon.getString("sum"));
				if (sum > maxSum) {
					flag = 1;
					maxSum = sum;
					aIndex = j;
				}
			}
		}
		JSONObject cJson = new JSONObject();
		if (flag == 1) {
			cJson.put("commend_coupon", jaCoupons.getJSONObject(aIndex));
		}
		cJson.put("coupons", jaCoupons);
		return cJson.toString();	
	}

	// 评价订单
	public static String evaluateOrder(JSONObject json) {
		String aJson = PostJson.postJson(json, EVALUATE_ORDER);
		return aJson;
	}

	// 立减优惠
	public static String availableSale(JSONObject json) {
		String aJson = PostJson.postJson(json, GET_AVAILABLE_SALE);
		return aJson;
	}

	// 派车时间
	public static String getScheduleTime(JSONObject json) {
		String aJson = PostJson.postJson(json, SCHEDULE_TIME);
		return aJson;
	}

}